home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / SNNSV32.ZIP / SNNSv3.2 / kernel / sources / kr_pat_parse.y < prev    next >
Text File  |  1994-04-25  |  9KB  |  408 lines

  1. /*****************************************************************************
  2.   FILE           : kr_pat_parse.y
  3.   SHORTNAME      : pat_parse
  4.   SNNS VERSION   : 3.2
  5.  
  6.   PURPOSE        : parser for new pattern format; bison format 
  7.   NOTES          : impossible to use with yacc
  8.  
  9.   AUTHOR         : Michael Vogt
  10.   DATE           : 10.9.93
  11.  
  12.   CHANGED BY     : 
  13.   IDENTIFICATION : @(#)kr_pat_parse.y    1.4 4/14/94
  14.   SCCS VERSION   : 1.4
  15.   LAST CHANGE    : 4/14/94
  16.  
  17.              Copyright (c) 1990-1993  SNNS Group, IPVR, Univ. Stuttgart, FRG
  18.  
  19. ******************************************************************************/
  20. %{
  21. #include <stdio.h>
  22.  
  23. /* since the generated parser needs alloca we need to do some special things */
  24. /* for some architectures: */
  25.  
  26. #ifndef alloca
  27. #ifndef __GNUC__
  28.  
  29. #ifdef __hpux
  30. #include <malloc.h>
  31. #include <stdio.h>
  32. #endif /* __hpux */
  33.  
  34. #ifdef sparc
  35. #include <alloca.h>
  36. #endif /* sparc */
  37.  
  38. #endif /* GNU C. not defined  */
  39. #endif /* alloca not defined.  */
  40.  
  41. #include "kr_typ.h"
  42. #include "glob_typ.h"
  43. #include "kernel.h"
  44. #include "kr_pat_scan.h"
  45. #include "kr_newpattern.h"
  46. #include "kr_pat_parse.ph"
  47. %}
  48.  
  49. %union
  50. {
  51.     float    value;        /* allgemeine Zahl */    
  52.     struct
  53.     {
  54.         int    v;
  55.         int    r;
  56.     }    version;    /* Versionsnummer #.# */
  57. }
  58.  
  59. %token    L_BRACKET R_BRACKET
  60. %token    VERSION_HEADER
  61. %token    GENERATED_AT
  62. %token    NO_OF_PATTERN NO_OF_INPUT NO_OF_OUTPUT
  63. %token    NO_OF_VAR_IDIM NO_OF_VAR_ODIM
  64. %token    MAXIMUM_IDIM MAXIMUM_ODIM
  65. %token    ERROR PATTERNEND
  66.  
  67. %token    <value> NUMBER
  68. %token    <version> V_NUMBER
  69.  
  70. %%
  71.  
  72. pattern_file:    header
  73.         {    
  74.             current_pattern = 0;
  75.             next_pattern_is_input = 1;
  76.  
  77.             if (kr_np_AllocatePatternSet(&pattern_set, no_of_pattern)
  78.             != KRERR_NO_ERROR)
  79.             { 
  80.             yyerror("can't allocate memory"); 
  81.             YYABORT; 
  82.             }
  83.         }        
  84.         pattern_list
  85.         {
  86.             if (current_pattern < no_of_pattern ||
  87.             !next_pattern_is_input)
  88.             { 
  89.             yyerror("unexpected end of file"); 
  90.             YYABORT; 
  91.             }
  92.         }
  93. ;
  94.  
  95. header:    VERSION_HEADER V_NUMBER 
  96.     {
  97.         if (($2.v == CURRENT_VERSION_V && $2.r > CURRENT_VERSION_R) ||
  98.             $2.v > CURRENT_VERSION_V)
  99.         { 
  100.             yyerror("version of pattern file not supported"); 
  101.             YYABORT; 
  102.         }
  103.     }
  104.     GENERATED_AT NO_OF_PATTERN NUMBER
  105.     {
  106.         no_of_pattern = (int) $6;
  107.         if (no_of_pattern <= 0)
  108.         { 
  109.             yyerror("illegal number of pattern"); 
  110.             YYABORT; 
  111.         }
  112.     }
  113.         i_head o_head vi_head vo_head
  114. ;
  115.  
  116. i_head:    NO_OF_INPUT NUMBER
  117.     { 
  118.         no_of_input = (int) $2;
  119.         if (no_of_input < 0)
  120.         { 
  121.             yyerror("illegal number of input units"); 
  122.             YYABORT; 
  123.         } 
  124.     }
  125. ;
  126.  
  127. o_head:    NO_OF_OUTPUT NUMBER
  128.     { 
  129.         no_of_output = (int) $2;
  130.         if (no_of_output < 0)
  131.         { 
  132.             yyerror("illegal number of output units"); 
  133.             YYABORT; 
  134.         } 
  135.     }
  136.     |
  137.     { no_of_output = 0; }
  138. ;
  139.  
  140. vi_head: NO_OF_VAR_IDIM NUMBER MAXIMUM_IDIM actual_dim
  141.     {
  142.         variable_input_dim = $2; 
  143.         if (variable_input_dim < 0 || 
  144.             variable_input_dim > MAX_NO_OF_VAR_I_DIM)
  145.         { 
  146.             yyerror("illegal variable input dimensions"); 
  147.             YYABORT; 
  148.         }
  149.         if (actual_dim_count != variable_input_dim)
  150.         { 
  151.             yyerror("illegal number of entries in dimension list"); 
  152.             YYABORT; 
  153.         }
  154.         for (i=0; i<variable_input_dim; i++)
  155.             max_i_dims[i] = dims[i];
  156.     }
  157.     |
  158.     { variable_input_dim = 0; }
  159. ;
  160.  
  161. vo_head: NO_OF_VAR_ODIM NUMBER MAXIMUM_ODIM actual_dim
  162.     {
  163.         variable_output_dim = $2; 
  164.         if (variable_output_dim < 0 || 
  165.             variable_output_dim > MAX_NO_OF_VAR_O_DIM ||
  166.             no_of_output == 0)
  167.         { 
  168.             yyerror("illegal variable output dimensions"); 
  169.             YYABORT; 
  170.         }
  171.         if (actual_dim_count != variable_output_dim)
  172.         { 
  173.             yyerror("illegal number of entries in dimension list"); 
  174.             YYABORT; 
  175.         }
  176.         for (i=0; i<variable_output_dim; i++)
  177.             max_o_dims[i] = dims[i];    
  178.     }
  179.     |
  180.     { variable_output_dim = 0; }
  181. ;
  182.  
  183. actual_dim: L_BRACKET 
  184.         {
  185.             actual_dim_count = 0;
  186.         }
  187.         actual_dim_rest R_BRACKET
  188.         | L_BRACKET R_BRACKET                
  189.                 {
  190.                 actual_dim_count = 0;
  191.         }
  192. ;
  193.  
  194. actual_dim_rest: dim_entry
  195.     | actual_dim_rest dim_entry
  196. ;
  197.  
  198. dim_entry: NUMBER
  199.     {
  200.         if (actual_dim_count >= MAX_NO_OF_VAR_DIM)
  201.         { 
  202.             yyerror("to many entries in dimension list"); 
  203.             YYABORT; 
  204.         }
  205.         dims[actual_dim_count] = (int) $1;
  206.         if (dims[actual_dim_count] <= 0)
  207.         { 
  208.             yyerror("illegal size of dimension"); 
  209.             YYABORT; 
  210.         }
  211.         actual_dim_count++;
  212.     }
  213. ;
  214.  
  215. pattern_list: pattern
  216.     |     pattern_list pattern
  217. ;
  218.  
  219. pattern: 
  220. {
  221.     if (current_pattern >= no_of_pattern)
  222.     { 
  223.     yyerror("to many patterns"); 
  224.     YYABORT; 
  225.     }
  226. pattern_start 
  227. {
  228.     if (kr_np_GetDescriptor(pattern_set, current_pattern, &pattern)
  229.     != KRERR_NO_ERROR)
  230.     {
  231.     yyerror("pattern parser internal error");
  232.     YYABORT;
  233.     }
  234.     if (next_pattern_is_input)
  235.     {
  236.     pattern -> input_fixsize = no_of_input;
  237.     pattern -> input_dim = variable_input_dim;
  238.     pattern -> output_fixsize = no_of_output;
  239.     pattern -> output_dim = variable_output_dim;
  240.     if (actual_dim_count != variable_input_dim)
  241.     { 
  242.         yyerror("illegal number of entries in dimension list"); 
  243.         YYABORT; 
  244.     }
  245.     act_size = no_of_input;
  246.     for (i=0; i<variable_input_dim; i++)
  247.     {
  248.         if (dims[i] > max_i_dims[i])
  249.         { 
  250.         yyerror("variable dimension overflow"); 
  251.         YYABORT; 
  252.         }
  253.         act_size *= dims[i];
  254.         (pattern -> input_dim_sizes)[i] = dims[i];
  255.     }
  256.     if (kr_np_AllocatePattern(pattern, next_pattern_is_input)
  257.         != KRERR_NO_ERROR)
  258.     { 
  259.         yyerror("can't allocate memory"); 
  260.         YYABORT; 
  261.     }
  262.     pat_mem = pattern -> input_pattern;
  263.     }
  264.     else
  265.     {
  266.     if (actual_dim_count != variable_output_dim)
  267.     { 
  268.         yyerror("illegal number of entries in dimension list"); 
  269.         YYABORT; 
  270.     }
  271.     act_size = no_of_output;
  272.     for (i=0; i<variable_output_dim; i++)
  273.     {
  274.         if (dims[i] > max_o_dims[i])
  275.         { 
  276.         yyerror("variable dimension overflow"); 
  277.         YYABORT; 
  278.         }
  279.         act_size *= dims[i];
  280.         (pattern -> output_dim_sizes)[i] = dims[i];
  281.     }
  282.     if (kr_np_AllocatePattern(pattern, next_pattern_is_input)
  283.         != KRERR_NO_ERROR)
  284.     { 
  285.         yyerror("can't allocate memory"); 
  286.         YYABORT; 
  287.     }
  288.     pat_mem = pattern -> output_pattern;
  289.     }
  290. }
  291. pattern_body 
  292. {
  293.     if (act_size > 0)
  294.     { 
  295.     yyerror("to little values in pattern"); 
  296.     YYABORT; 
  297.     }
  298.     
  299.     if (no_of_output > 0)
  300.     next_pattern_is_input = !next_pattern_is_input;
  301.     
  302.     if (next_pattern_is_input)
  303.     current_pattern++;
  304. }
  305. PATTERNEND
  306. ;
  307.  
  308. pattern_start: actual_dim
  309.         | /* empty */
  310. {
  311.     actual_dim_count = 0;
  312. }
  313. ;
  314.  
  315.  
  316. pattern_body: pattern_entry
  317.     |     pattern_body pattern_entry
  318. ;
  319.  
  320. pattern_entry: NUMBER
  321.     {
  322.         if (act_size == 0)
  323.         { 
  324.             yyerror("to many values in pattern"); 
  325.             YYABORT; 
  326.         }
  327.         *pat_mem++ = $1;
  328.         if (--act_size == 0)
  329.             scanner_await_pattern_end(); 
  330.     }
  331. ;
  332.  
  333.  
  334. %%
  335.  
  336. #ifndef alloca
  337. #ifndef __GNUC__
  338. #ifdef __hpux
  339. /*****************************************************************************
  340.   FUNCTION : alloca
  341.  
  342.   PURPOSE  : must be provided for the parser.
  343.              Uses malloc to allocate memory. This memory is not automatically
  344.              freed as in the original alloca definition 
  345.   RETURNS  : pointer to memory
  346.   NOTES    : This is necessary for HP_UX only since there comes no alloca() 
  347.              with the HP cc C-compiler.
  348.          It is not necessary with Gnu C
  349.  
  350.   UPDATE   : 
  351. ******************************************************************************/
  352. void *alloca(int size)
  353. {
  354.     void *p;
  355.  
  356.     p = malloc(size);
  357.     fprintf(stderr, "warning: malloc called instead of alloca\n");
  358.     return p;
  359. }
  360. #endif /* __hpux */
  361. #endif /* GNU C. not defined  */
  362. #endif /* alloca not defined.  */
  363.  
  364. /*****************************************************************************
  365.   FUNCTION : yyerror
  366.  
  367.   PURPOSE  : must be provided for the parser.
  368.              reports errors in pattern file to stderr
  369.   RETURNS  : nothing
  370.   NOTES    :
  371.  
  372.   UPDATE   : 
  373. ******************************************************************************/
  374. static void yyerror(char *error)
  375. {
  376.     fprintf(stderr, "Parse error in pattern file at line %d:\n%s\n",
  377.         lineno, error);
  378. }
  379.  
  380. /*****************************************************************************
  381.   FUNCTION : parse_pattern_file
  382.  
  383.   PURPOSE  : calls the real parser
  384.  
  385.   RETURNS  : parser error code and the handle to the loaded pattern set
  386.   NOTES    :
  387.  
  388.   UPDATE   : 
  389. ******************************************************************************/
  390. int parse_pattern_file(int *set)
  391. {
  392.     int err;
  393.  
  394.     lineno = 1;
  395.     err = yyparse();
  396.     *set = pattern_set;
  397.  
  398.     if (err == 0)
  399.     lineno = 0;
  400.  
  401.     return err;
  402. }
  403.  
  404. /*****************************************************************************
  405. END OF FILE
  406. ******************************************************************************/
  407.